package com.peanut.boot.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * 拦截器——登录
 *1  实现 implements HandlerInterceptor
 *2  注册拦截器
 *3  指定拦截规则 如果是全部拦截， 注意放行静态资源。
 */
public class loginInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (request.getSession().getAttribute("user") != null) {
			return true;
		} else {
			//没有登录的转发到登录页面
			request.setAttribute("msg","非法访问,请登录。");
			request.getRequestDispatcher("/").forward(request,response);
			return false;
		}
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
	
	}
}
